package com.anosine.gpsl.android.util;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

/**
 * @Title: SendHttpUtils<T>
 * @Package com.anosine.gpsl.android.util
 * @Description: 
 * @author: sunny
 * @date: 2015-11-19
 * @version V1.0
 */
public class SendHttpUtils<T> {

	/**
	 * 方法名：doRequest(String url, RequestParams params, final TypeToken<T> type, final HttpRequestListener listener)
	 * 功能：解析json语句
	 * @param url 服务器访问地址
	 * @param params 传给服务器的参数
	 * @param type 要解析成的实体类型
	 * @param HttpRequestListener listener 请求监听，接口实现
	 * @return 无
	 * */
    public void doRequest(final String url, RequestParams params, final TypeToken<T> type, final HttpRequestListener<T> listener){
        if (NetStateManager.OnNet()){
            HttpUtils http = new HttpUtils();
            http.configCurrentHttpCacheExpiry(1000 * 5);
            //通过HTTP来发送post请求， 并书写回调函数  
            http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            	 /**
                 * 方法名：onSuccess(ResponseInfo<String> objectResponseInfo)
                 * 功	能：解析成功后做的操作
                 * @param objectResponseInfo 返回数据
                 * @return none
                 * */
            	@Override
                public void onSuccess(ResponseInfo<String> objectResponseInfo) {
                    boolean isSuccess=false;
                    String errorInfo=null;
                    try {
                        String result = objectResponseInfo.result;
                        if (StringUtil.isNotEmpty(result)){
                            T t= null;
                            try {
                                if (listener!=null){
                                    try {
                                        if (type!=null && StringUtil.isNotEmpty(result)){
                                            t = (T) new Gson().fromJson(result, type.getType());                                            
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            Log.i("TAG", result+t);
                            listener.onSuccess(result, t);
                           
                            isSuccess=true;
                        }
                    } catch (Exception e) {
                    	Log.i("TAG", e.toString());                    	
                    }
                    if (!isSuccess){
                    	Log.i("TAG22", url);
                        if (StringUtil.isEmptyOrNull(errorInfo)){
                            errorInfo="数据返回异常";
                        }
                        listener.onFailure(errorInfo);
                    }
                }
                /**
                 * 方法名：onFailure(HttpException e, String s) 
                 * 功	能：解析失败后做的操作
                 * @param HttpException e 异常
                 * @param String s 错误信息
                 * @return none
                 * */
                @Override
                public void onFailure(HttpException e, String s) {
                    String error=null;
                    if (StringUtil.isEmptyOrNull(s)){
                        error=s;
                    }else{
                        error="加载失败，请稍候重试！";
                    }
                    if (listener!=null){
                        listener.onFailure(error);
                    }
                }
            });

        }else{
            String error="加载失败，请联网后重试！";
            if (listener!=null){
                listener.onFailure(error);
            }
            
        }
    }

}
